package erfgame.core.util;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import erfgame.core.util.RangeUtils;

public class UnionRangeTester {

	@Test
	public void testUnionLeft() {
		short[] i = {0,2,0,0};
		short[] w = {4,6};
		short[] er = {0,2,4,6};
		int count = RangeUtils.unionRange( i, 2, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testUnionRight() {
		short[] i = {4,6,0,0};
		short[] w = {0,2};
		short[] er = {0,2,4,6};
		int count = RangeUtils.unionRange( i, 2, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testMergeLeft() {
		short[] i = {0,3};
		short[] w = {3,6};
		short[] er = {0,6};
		int count = RangeUtils.unionRange( i, 2, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testMergeRight() {
		short[] i = {3,6};
		short[] w = {0,3};
		short[] er = {0,6};
		int count = RangeUtils.unionRange( i, 2, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testOverwrite() {
		short[] i = {2,4};
		short[] w = {0,6};
		short[] er = {0,6};
		int count = RangeUtils.unionRange( i, 2, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testSubset() {
		short[] i = {0,6};
		short[] w = {2,4};
		short[] er = {0,6};
		int count = RangeUtils.unionRange( i, 2, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testDouble() {
		short[] i = {0,2,8,10};
		short[] w = {2,4,6,8};
		short[] er = {0,4,6,10};
		int count = RangeUtils.unionRange( i, 4, w, w.length );
		assertEqual( er, i, count );
	}

	@Test
	public void testDuplicates() {
		short[] i = {0,3,5,8};
		short[] w = {2,6};
		short[] er = {0,8};
		int count = RangeUtils.unionRange( i, 4, w, w.length );
		assertEqual( er, i, count );
	}

	private void assertEqual( short[] in, short[] out, int length ) {
		assertEquals( in.length, length );
		for( int i=0; i<length; i++ ) {
			assertEquals( in[i], out[i] );
		}
	}

}
